/*
 * @Author: xiaosihan 
 * @Date: 2022-09-29 15:33:06 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2022-12-27 11:49:54
 */


import { Mesh, MeshStandardMaterial, DoubleSide } from "three";
import threeLoader from "three-base/threeLoader";
import threeUtils from "three-base/threeUtils";
import GLTF_GLB from "./GLTF.glb?url";



// 加载 gltf格式的模型 例子
export default class GLTF extends Mesh {
    constructor() {
        super();

        this.loadModal();
    }

    // 加载模型
    async loadModal() {
        const obj = await threeLoader.gltfLoader.loadAsync(GLTF_GLB);

        obj.scene.traverse(o => {
            const mesh = (o as Mesh);
            if (mesh.isMesh) {
                (mesh.material as MeshStandardMaterial).side = DoubleSide;
            }
        })

        threeUtils.scaleTo(obj.scene, 1);
        threeUtils.centerObject3D(obj.scene);

        this.add(obj.scene);

        // 加载完成事件
        this.dispatchEvent({ type: "loaded" } as never);
    }

}